package top.blentle.ai.data.api.controller;


import okio.BufferedSource;
import okio.Okio;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.blentle.ai.data.AiService;
import top.blentle.ai.data.api.dto.LampDTO;
import top.blentle.ai.data.api.dto.LampData;
import top.blentle.ai.data.api.dto.LampEvent;
import top.blentle.ai.data.util.JsonMapper;
import top.blentle.ai.data.util.JsonMsg;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Topic;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

@Controller
@RequestMapping("/ai")
@CrossOrigin(origins = "*")
public class AiController {

    private static Logger logger = LoggerFactory.getLogger(AiController.class);

    @Resource
    private AiService aiService;

    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Resource
    private Topic reportTopic;

    @RequestMapping("/data/show")
    @ResponseBody
    public String showData(HttpServletRequest request) throws IOException {
        String encoding = request.getCharacterEncoding();
        ServletInputStream is = request.getInputStream();
        int total = request.getContentLength();
        String type = request.getContentType();
        logger.info("length：【{}】, type : 【{}】, encoding is :【{}】", total, type, encoding);
        if (total > -1) {
            BufferedSource bs = Okio.buffer(Okio.source(is));
            byte head = bs.readByte();
            String deviceId = bs.readByteString(12).toString();
            logger.error("head:【{}】, deviceId :【{}】", head, deviceId);
            bs.close();
        }
        String s = "hello,,收到来自客户端的消息了,total length: " + total + "\rtype: " + type + "\rbody byte data pointer address: " + is;
        return s;
    }

    @RequestMapping("/topic")
    @ResponseBody
    public String topic() {
        for (int i = 0; i < 10; i++) {
            jmsMessagingTemplate.convertAndSend(reportTopic, "topic" + i);
        }
        return "topic 发送成功";
    }

    @RequestMapping("/data/pull")
    @ResponseBody
    public String pull(@RequestBody LampDTO dto) {
        JsonMsg msg = new JsonMsg();
        msg.setStatus(1);
        LampData data = new LampData();
        data.setDeviceId(dto.getDeviceId());
        data.setVersionId(dto.getVersion());
        data.setRequestTime(new Date(dto.getRequestTime()));
        data.setResponseTIme(new Date());
        Random random = new Random();
        int power = random.nextInt(100);
        data.setPower(power);
        data.setColor(random.nextInt(3));
        data.setPowerType(0);
        data.setScene(0);
        data.setState(random.nextInt(3));
        msg.setData(data);
        return JsonMapper.toJson(msg);
    }


    @RequestMapping("/event/push")
    @ResponseBody
    public String push(@RequestBody LampEvent dto) {
        //触发事件
        JsonMsg msg = null;
        try {
            dto.setRequestTime(System.currentTimeMillis());
            msg = aiService.triggerEvent(dto);
            msg.setStatus(1);
        } catch (Exception e) {
            msg.setStatus(0);
            logger.error("send command error", e);
        }
        return JsonMapper.toJson(msg);
    }

}
